class Api::V1::ActionsController < Api::V1::BaseController
	prepend_before_filter :signed_in_user
	before_action :set_action, only: [:show, :edit, :update, :destroy]
	before_action :find_stakeholder, only: [:index]

	def index
		if params[:stakeholder_id]
			@actions = @stakeholder.actions
		else
			@actions = Action.all
		end
	end

	def create
		@action = Action.new(action_params)
		if @action.save
			render action: "show"
		else
			render :status => 406,
					:json => {
						:status_code => 406,
						:message => "Error creating resource!",
						:content => @action.errors.full_messages
					}
		end
	end

	def show
	end

	def update
		if @action.update(action_params)
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource updated succesfully!",
						:content => ""
					 }
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error updating resource!",
						:content => @action.errors.full_messages
					 }
		end
	end

	def destroy
		if @action.destroy
		render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource deleted succesfully!",
						:content => ""
					}
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error deleting resource!",
						:content => @action.errors.full_messages
					}
		end
	end

	private

		def set_action
     		@action = Action.find(params[:id])
     		rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => "" 
						 }
   		end

		def find_stakeholder
			if params[:stakeholder_id]
				@stakeholder = Stakeholder.find(params[:stakeholder_id])
			end
			rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => ""
						 }
		end

		def action_params
			params.require(:my_action).permit(:version_id, :stakeholder_id, :action)
		end
end